{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d70a6fda",
   "metadata": {},
   "source": [
    "### 尝试创建detect.py 并实现功能调用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "5c610072",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "API_key = \"6rDedGDfLFtMa8Uf6vVW7fMFkt-41hKf\"\n",
    "API_secret = \"4X0HqN7imkvMXCLx7y1BZXA6hUMj7UV3\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "ad7cab56",
   "metadata": {},
   "outputs": [],
   "source": [
    "import detect"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "ff451fc6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648481205,53e6324e-57b8-418f-aabc-ee3b78866be5',\n",
       " 'time_used': 199,\n",
       " 'faces': [{'face_token': '3fd8af1ff0cbfb97c9010c7a8f2baf96',\n",
       "   'face_rectangle': {'top': 318, 'left': 477, 'width': 355, 'height': 355}}],\n",
       " 'image_id': 'fgqMDWyVW5uUL9KwRXo04g==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect.face_detect(API_key,API_secret,\"02.jpeg\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a6e98fda",
   "metadata": {},
   "source": [
    "### 尝试创建analyze.py/campare.py/search.py,并实现功能调用"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "48bdf234",
   "metadata": {},
   "source": [
    "* analyze."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "49d2d314",
   "metadata": {},
   "outputs": [],
   "source": [
    "import analyze"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "f4622a1a",
   "metadata": {},
   "outputs": [],
   "source": [
    "face1 = detect.face_detect(API_key,API_secret,\"02.jpeg\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "8a4eec75",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'433cf387339399721f9c0d92d5b89d7e'"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_tokens1 = face1[\"faces\"][0][\"face_token\"]\n",
    "face_tokens1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "54a860ca",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on module analyze:\n",
      "\n",
      "NAME\n",
      "    analyze\n",
      "\n",
      "FUNCTIONS\n",
      "    face_analyze(API_key, API_secret, face_tokens, attributes=None, landmark=1)\n",
      "        该函数为调用face++ face_detect接口\n",
      "\n",
      "FILE\n",
      "    c:\\users\\zeng zihan\\api\\week-04\\analyze.py\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(analyze)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "1cff9616",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 107,\n",
       " 'request_id': '1648481432,d0cfbb0e-9171-4a2e-8289-be0c63e9de4c',\n",
       " 'faces': [{'landmark': {'mouth_upper_lip_left_contour2': {'y': 552, 'x': 693},\n",
       "    'mouth_upper_lip_top': {'y': 544, 'x': 735},\n",
       "    'mouth_upper_lip_left_contour1': {'y': 544, 'x': 722},\n",
       "    'left_eye_upper_left_quarter': {'y': 386, 'x': 604},\n",
       "    'left_eyebrow_lower_middle': {'y': 343, 'x': 625},\n",
       "    'mouth_upper_lip_left_contour3': {'y': 560, 'x': 700},\n",
       "    'right_eye_top': {'y': 358, 'x': 764},\n",
       "    'left_eye_bottom': {'y': 409, 'x': 627},\n",
       "    'right_eyebrow_lower_left_quarter': {'y': 327, 'x': 755},\n",
       "    'right_eye_pupil': {'y': 368, 'x': 764},\n",
       "    'mouth_lower_lip_right_contour1': {'y': 551, 'x': 755},\n",
       "    'mouth_lower_lip_right_contour3': {'y': 571, 'x': 756},\n",
       "    'mouth_lower_lip_right_contour2': {'y': 555, 'x': 765},\n",
       "    'contour_chin': {'y': 660, 'x': 734},\n",
       "    'contour_left9': {'y': 670, 'x': 688},\n",
       "    'left_eye_lower_right_quarter': {'y': 404, 'x': 647},\n",
       "    'mouth_lower_lip_top': {'y': 560, 'x': 735},\n",
       "    'right_eyebrow_upper_middle': {'y': 307, 'x': 764},\n",
       "    'left_eyebrow_left_corner': {'y': 358, 'x': 560},\n",
       "    'right_eye_bottom': {'y': 375, 'x': 770},\n",
       "    'contour_left7': {'y': 644, 'x': 596},\n",
       "    'contour_left6': {'y': 621, 'x': 554},\n",
       "    'contour_left5': {'y': 590, 'x': 520},\n",
       "    'contour_left4': {'y': 551, 'x': 494},\n",
       "    'contour_left3': {'y': 507, 'x': 479},\n",
       "    'contour_left2': {'y': 462, 'x': 473},\n",
       "    'contour_left1': {'y': 417, 'x': 472},\n",
       "    'left_eye_lower_left_quarter': {'y': 408, 'x': 607},\n",
       "    'contour_right1': {'y': 351, 'x': 805},\n",
       "    'contour_right3': {'y': 418, 'x': 818},\n",
       "    'contour_right2': {'y': 384, 'x': 812},\n",
       "    'mouth_left_corner': {'y': 557, 'x': 660},\n",
       "    'contour_right4': {'y': 453, 'x': 817},\n",
       "    'contour_right7': {'y': 557, 'x': 778},\n",
       "    'right_eyebrow_left_corner': {'y': 332, 'x': 740},\n",
       "    'nose_right': {'y': 474, 'x': 784},\n",
       "    'nose_tip': {'y': 475, 'x': 756},\n",
       "    'contour_right5': {'y': 489, 'x': 806},\n",
       "    'nose_contour_lower_middle': {'y': 505, 'x': 748},\n",
       "    'left_eyebrow_lower_left_quarter': {'y': 349, 'x': 592},\n",
       "    'mouth_lower_lip_left_contour3': {'y': 582, 'x': 708},\n",
       "    'right_eye_right_corner': {'y': 361, 'x': 788},\n",
       "    'right_eye_lower_right_quarter': {'y': 369, 'x': 780},\n",
       "    'mouth_upper_lip_right_contour2': {'y': 538, 'x': 760},\n",
       "    'right_eyebrow_lower_right_quarter': {'y': 317, 'x': 782},\n",
       "    'left_eye_left_corner': {'y': 402, 'x': 590},\n",
       "    'mouth_right_corner': {'y': 537, 'x': 770},\n",
       "    'mouth_upper_lip_right_contour3': {'y': 549, 'x': 754},\n",
       "    'right_eye_lower_left_quarter': {'y': 377, 'x': 757},\n",
       "    'left_eyebrow_right_corner': {'y': 334, 'x': 692},\n",
       "    'left_eyebrow_lower_right_quarter': {'y': 341, 'x': 658},\n",
       "    'right_eye_center': {'y': 368, 'x': 767},\n",
       "    'nose_left': {'y': 498, 'x': 688},\n",
       "    'mouth_lower_lip_left_contour1': {'y': 562, 'x': 700},\n",
       "    'left_eye_upper_right_quarter': {'y': 382, 'x': 648},\n",
       "    'right_eyebrow_lower_middle': {'y': 321, 'x': 769},\n",
       "    'left_eye_top': {'y': 378, 'x': 625},\n",
       "    'left_eye_center': {'y': 397, 'x': 627},\n",
       "    'contour_left8': {'y': 661, 'x': 641},\n",
       "    'contour_right9': {'y': 630, 'x': 757},\n",
       "    'right_eye_left_corner': {'y': 377, 'x': 745},\n",
       "    'mouth_lower_lip_bottom': {'y': 582, 'x': 737},\n",
       "    'left_eyebrow_upper_left_quarter': {'y': 333, 'x': 587},\n",
       "    'left_eye_pupil': {'y': 394, 'x': 623},\n",
       "    'right_eyebrow_upper_left_quarter': {'y': 316, 'x': 748},\n",
       "    'contour_right8': {'y': 593, 'x': 765},\n",
       "    'right_eyebrow_right_corner': {'y': 314, 'x': 794},\n",
       "    'right_eye_upper_left_quarter': {'y': 366, 'x': 752},\n",
       "    'left_eyebrow_upper_middle': {'y': 322, 'x': 622},\n",
       "    'right_eyebrow_upper_right_quarter': {'y': 304, 'x': 780},\n",
       "    'nose_contour_left1': {'y': 388, 'x': 687},\n",
       "    'nose_contour_left2': {'y': 463, 'x': 693},\n",
       "    'mouth_upper_lip_right_contour1': {'y': 539, 'x': 746},\n",
       "    'nose_contour_right1': {'y': 377, 'x': 740},\n",
       "    'nose_contour_right2': {'y': 445, 'x': 769},\n",
       "    'mouth_lower_lip_left_contour2': {'y': 572, 'x': 683},\n",
       "    'contour_right6': {'y': 523, 'x': 793},\n",
       "    'nose_contour_right3': {'y': 492, 'x': 771},\n",
       "    'nose_contour_left3': {'y': 505, 'x': 718},\n",
       "    'left_eye_right_corner': {'y': 397, 'x': 665},\n",
       "    'left_eyebrow_upper_right_quarter': {'y': 320, 'x': 659},\n",
       "    'right_eye_upper_right_quarter': {'y': 357, 'x': 777},\n",
       "    'mouth_upper_lip_bottom': {'y': 557, 'x': 735}},\n",
       "   'face_rectangle': {'width': 355, 'top': 318, 'left': 477, 'height': 355},\n",
       "   'face_token': '433cf387339399721f9c0d92d5b89d7e'}]}"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "analyze.face_analyze(API_key,API_secret,face_tokens1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c472c662",
   "metadata": {},
   "source": [
    "* compare"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "dd6e4fea",
   "metadata": {},
   "outputs": [],
   "source": [
    "import compare"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "556ef6ca",
   "metadata": {},
   "outputs": [],
   "source": [
    "face2 = detect.face_detect(API_key,API_secret,\"01.jpeg\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "d05249b3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'43c17053a043308a4cc02f0ea07c1f4d'"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_tokens2 = face2[\"faces\"][0][\"face_token\"]\n",
    "face_tokens2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "30fb7813",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on module compare:\n",
      "\n",
      "NAME\n",
      "    compare\n",
      "\n",
      "FUNCTIONS\n",
      "    face_compare(API_key, API_secret, face_token1, face_token2)\n",
      "        该函数为调用face++ face_compare接口\n",
      "\n",
      "FILE\n",
      "    c:\\users\\zeng zihan\\api\\week-04\\compare.py\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(compare)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0542606d",
   "metadata": {},
   "source": [
    "* search"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "84793e28",
   "metadata": {},
   "outputs": [],
   "source": [
    "import search"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "8c0babf1",
   "metadata": {},
   "outputs": [],
   "source": [
    "base_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/create\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "ef9377af",
   "metadata": {},
   "outputs": [],
   "source": [
    "payload = {\n",
    "    \"api_key\":API_key,\n",
    "    \"api_secret\":API_secret,\n",
    "    \"face_tokens\":\"12726c4ad378d7835fcd7ed32bc8cbda\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "3929c2ba",
   "metadata": {},
   "outputs": [],
   "source": [
    "r = requests.post(base_url,params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "171eeadb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '4eb0e0f5bdc79aba4d3d470b0349fdbe',\n",
       " 'time_used': 179,\n",
       " 'face_count': 0,\n",
       " 'face_added': 0,\n",
       " 'request_id': '1648481818,83947087-1b71-4f7d-9142-f7307c4fb95f',\n",
       " 'outer_id': '',\n",
       " 'failure_detail': [{'reason': 'INVALID_FACE_TOKEN',\n",
       "   'face_token': '12726c4ad378d7835fcd7ed32bc8cbda'}]}"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "ba5aa66b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'4eb0e0f5bdc79aba4d3d470b0349fdbe'"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "faceset_tokens = r.json()[\"faceset_token\"]\n",
    "faceset_tokens"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "12463400",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 74,\n",
       " 'error_message': 'EMPTY_FACESET',\n",
       " 'request_id': '1648481857,9f824e39-e412-485d-b5fe-f61109523c42'}"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "search.face_search(API_key,API_secret,faceset_tokens,face_tokens1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2ace986d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
